package org.tissou.server.Tests;

import com.google.gwt.user.client.ui.Grid;
import junit.framework.TestCase;
import org.tissou.client.Services.Parametres;
import org.tissou.client.Services.MonException;
import org.tissou.client.Services.OperationFactoryInfos;
import org.tissou.client.Services.Compte;
import org.tissou.server.Operations.OperationFactory;
import org.tissou.server.Utils.Variables_Globales;
import org.tissou.server.Utils.InterfaceMySQL;
import org.tissou.server.Base.Monnaie;

import java.util.Date;
import java.util.logging.Logger;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: Claire
 * Date: 03/03/2009
 * Time: 21:12:32
 * To change this template use File | Settings | File Templates.
 */
public class EntreeOperationDetteTest extends TestCase {
  private static final Logger LOG = Logger.getLogger(EntreeOperationDetteTest.class.getName());

  Parametres parametres_dette1 = new Parametres(new Date(),true, OperationFactoryInfos.TypeOperation.OPERATION_PRET);
  String string1;
  Parametres parametres_dette2 = new Parametres(new Date(),true, OperationFactoryInfos.TypeOperation.OPERATION_PRET);
  String string2;
   Parametres parametres_dette3 = new Parametres(new Date(),true, OperationFactoryInfos.TypeOperation.OPERATION_REMBOURSEMENT);
  String string3;
  Parametres parametres_dette4 = new Parametres(new Date(),true, OperationFactoryInfos.TypeOperation.OPERATION_REMBOURSEMENT);
  String string4;

   public void testEntrerTransaction1() throws MonException {
     formerParametres1();
    try {
    OperationFactory.CreerOperation(parametres_dette1).Entrer_Transactions();
    } catch (MonException e) {
      throw e;
    } catch (NullPointerException e) {
      throw new MonException("Desole mais ya un truc qui etait tout nul et du coup j'ai pas pu gerer.");
    }
     try {
      ResultSet result = InterfaceMySQL.getInstance().RecupererDonnees("SELECT Categorie FROM transactions WHERE (NoTransaction = (SELECT MAX(NoTransaction) FROM transactions))");
      while(result.next()){
        string1 = result.getString("Categorie");
        LOG.info(string1);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    assertEquals("Pret", string1);
  }

   public void testEntrerTransaction2() throws MonException {
     formerParametres2();
    try {
    OperationFactory.CreerOperation(parametres_dette2).Entrer_Transactions();
    } catch (MonException e) {
      throw e;
    } catch (NullPointerException e) {
      throw new MonException("Desole mais ya un truc qui etait tout nul et du coup j'ai pas pu gerer.");
    }
     try {
      ResultSet result = InterfaceMySQL.getInstance().RecupererDonnees("SELECT Categorie FROM transactions WHERE (NoTransaction = (SELECT MAX(NoTransaction) FROM transactions))");
      while(result.next()){
        string2 = result.getString("Categorie");
        LOG.info(string2);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    assertEquals(string2, "Loisir");
  }

  public void testEntrerTransaction3() throws MonException {
     formerParametres3();
    try {
    OperationFactory.CreerOperation(parametres_dette3).Entrer_Transactions();
    } catch (MonException e) {
      throw e;
    } catch (NullPointerException e) {
      throw new MonException("Desole mais ya un truc qui etait tout nul et du coup j'ai pas pu gerer.");
    }
     try {
      ResultSet result = InterfaceMySQL.getInstance().RecupererDonnees("SELECT Categorie FROM transactions WHERE (NoTransaction = (SELECT MAX(NoTransaction) FROM transactions))");
      while(result.next()){
        string3 = result.getString("Categorie");
        LOG.info(string3);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    assertEquals("Remboursement", string3);
  }



  public void testEntrerTransaction4() throws MonException {
     formerParametres4();
    try {
    OperationFactory.CreerOperation(parametres_dette4).Entrer_Transactions();
    } catch (MonException e) {
      throw e;
    } catch (NullPointerException e) {
      throw new MonException("Desole mais ya un truc qui etait tout nul et du coup j'ai pas pu gerer.");
    }
     try {
      ResultSet result = InterfaceMySQL.getInstance().RecupererDonnees("SELECT Categorie FROM transactions WHERE (NoTransaction = (SELECT MAX(NoTransaction) FROM transactions))");
      while(result.next()){
        string4 = result.getString("Categorie");
        LOG.info(string4);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    assertEquals("Hygiene", string4);
  }



  private void formerParametres1() {
    parametres_dette1.ajouterCompte(Variables_Globales.ListeProprietaire.ObtenirProprietaire("Claire").ObtenirCompte("BNP (euro)"));
    parametres_dette1.ajouterCompte(Variables_Globales.ListeProprietaire.ObtenirProprietaire("Manu").ObtenirCompte("HSBC (euro)"));
    parametres_dette1.ajouterSomme(10f);
  }

   private void formerParametres2() {
     parametres_dette2.ajouterCategorie(Variables_Globales.Tout.rechercherCategorieDansDescendance("Loisir").getInfos());
     parametres_dette2.ajouterCompte(Variables_Globales.ListeProprietaire.ObtenirProprietaire("Claire").ObtenirCompte("BNP (euro)"));
    parametres_dette2.ajouterCompte(Variables_Globales.ListeProprietaire.ObtenirProprietaire("Manu").ObtenirCompte("HSBC (euro)"));
    parametres_dette2.ajouterSomme(10f);
  }

  private void formerParametres3() {
   parametres_dette3.ajouterCompte(Variables_Globales.ListeProprietaire.ObtenirProprietaire("Claire").ObtenirCompte("BNP (euro)"));
    parametres_dette3.ajouterCompte(Variables_Globales.ListeProprietaire.ObtenirProprietaire("Manu").ObtenirCompte("HSBC (euro)"));
    parametres_dette3.ajouterSomme(150f);
  }

  private void formerParametres4() {
    parametres_dette4.ajouterCategorie(Variables_Globales.Tout.rechercherCategorieDansDescendance("Hygiene").getInfos());
    parametres_dette4.ajouterCompte(Variables_Globales.ListeProprietaire.ObtenirProprietaire("Claire").ObtenirCompte("BNP (euro)"));
    parametres_dette4.ajouterCompte(Variables_Globales.ListeProprietaire.ObtenirProprietaire("Manu").ObtenirCompte("HSBC (euro)"));
    parametres_dette4.ajouterSomme(250f);
  }

}
